/**
 * Created by sunkejun on 2015/10/15.
 */

$(document).ready(function(){

    var table = new ComDataTable("/admin/tag/page",$('#dataTable'),"admin_post_tags");
    var renders = {
        id:admin_tags_list.editRender
    };
    table.setRenders(renders);
    table.init();
    admin_tags_list.setTable(table);
})
var admin_tags_list  = (function ($) {

    this.editRender = function( data, type, full, meta) {

        return '<div class=" btn-group">'
            +'<a class="btn btn-xs btn-white" onclick="admin_tags_list.editTarget('+data+')" ><i class="fa fa-wrench"></i> </a>'
            +' <a class="btn btn-xs btn-white" onclick="admin_tags_list.delete('+data+')"><i class="fa fa-minus"></i> </a>'
            +'</div>';
        //return '<a data-toggle="tooltip" tooltip="hello" onclick="admin_tags_list.editTarget('+data+')" ><i class="fa fa-wrench"></i></a>'+
        //    '<a data-toggle="tooltip" tooltip="hello" onclick="admin_tags_list.delete('+data+')" ><i class="fa fa-minus"></i></a>'
        //    ;
    }

    this.delete = function(id){
        ComdataUtil.ajaxpostnormal('/admin/tag/'+id,{},this.finishAjaxLoadBack,"DELETE");
    }
    this.setTable = function(table){
        this.$table = table;
    }
    this.addNew = function(){
        openModel();
    }
    this.getItem = function(item){
        //openModel();
        ComdataUtil.loadContainer("tagform",item);
    }
    this.editTarget = function(id){
        openModel();
        ComdataUtil.ajaxpostnormal('/admin/tag/'+id,{},this.getItem,"GET");

    }

    this.openModel = function() {
         $("#tag_modal").modal('show');
    }
    this.closeModel = function() {
         $("#tag_modal").modal('hide');
    }


    this.submit = function(){
        $('#com_message').html("");
        var data = ComdataUtil.collectData("tagform");
        ComdataUtil.ajaxpost('/admin/tag',data,this.finishAjaxLoadBack);
    }
    this.finishAjaxLoadBack = function(response){
        if(response.code == 500){
            $('#com_message').attr("class","text-danger text-center");
            $('#com_message').html(response.res_data);
        }else{
            ComdataUtil.clear("tagform");
            closeModel();
            this.$table.rdraw();
        }

    }
    this.resetCategory = function(){
       ComdataUtil.clear("tagform");
    }

    return this;
})(jQuery);

